package face.pay.api.payter.channel;

import com.alibaba.fastjson.JSONObject;
import face.pay.api.payter.channel.paytype.alilpay.AliFacePay;
import face.pay.api.payter.channel.paytype.alilpay.AliJsPay;
import face.pay.api.payter.channel.paytype.wechat.AliWxQrPay;
import face.pay.constant.ChannelCode;
import face.pay.constant.PayClient;
import face.pay.entity.TerminalInfo;
import face.pay.api.payter.channel.paytype.alilpay.AliQrPay;
import face.pay.util.ResultMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 支付宝刷脸支付
 */
@Component
public class AliPayChannel extends AbstractChannel {

    private  AliJsPay aliJsPay;

    private  AliWxQrPay wxQrPay;

    private  AliQrPay aliQrPay;

    private  AliFacePay aliFacePay;

    public AliPayChannel() {
    }


    @Autowired
    public AliPayChannel(AliJsPay aliJsPay, AliWxQrPay wxQrPay, AliQrPay aliQrPay, AliFacePay aliFacePay) {
        this.aliJsPay = aliJsPay;
        this.wxQrPay = wxQrPay;
        this.aliQrPay = aliQrPay;
        this.aliFacePay = aliFacePay;
    }

    @Override
    public String getChannelCode() {
       return ChannelCode.ALIPAY;
    }
    @Override
    public ResultMessage facePay(JSONObject json) {

        return ResultMessage.createFail("方法已经费用");
    }

    @Override
    public ResultMessage qrPay(JSONObject json) {

        /**
         * 设备验证
         */
        String device_id = json.getString("device_id")==null?"":json.getString("device_id");
        String imei = json.getString("imei")==null?"":json.getString("imei");



        TerminalInfo terminalInfo  = this.terminalInfoValidate(device_id,imei);

        if(terminalInfo!=null){

            String auth_code = json.getString("auth_code");

            String barType = this.barType(auth_code);

            json.put("terminalId",terminalInfo.getId());
            json.put("channelCode",getChannelCode());
            String codeType  =  json.getString("codeType");

            if(PayClient.ALIPAY.equals(barType)){

                if("F".equals(codeType)){
                     return aliFacePay.pay(json);
                }else {
                    return  aliQrPay.pay(json);

                }
            }else if(PayClient.WECHAT.equals(barType)){
                return  wxQrPay.pay(json);
            }else {
                return  ResultMessage.createFail("付款码识别错误");
            }

        }else {
            return ResultMessage.createFail("设备信息验证有误，检查登录状态以及门店信息");
        }

    }

    @Override
    public ResultMessage jsPay(JSONObject json) {


        /**
         * 设备验证
         */
        String device_id = json.getString("device_id")==null?"":json.getString("device_id");

        TerminalInfo terminalInfo  = this.getTermial(device_id);

        if(terminalInfo!=null && terminalInfo.getStoreId()!=null){

            json.put("terminalId",terminalInfo.getId());
            json.put("channelCode",getChannelCode());
            json.put("storeId",terminalInfo.getStoreId());

            return aliJsPay.pay(json);

        }else {
            return  ResultMessage.createFail("设备不存在或设备未绑定门店");
        }
    }
}
